<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="adposcommList">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="广告位置" align="center" prop="name" />
      <el-table-column label="广告名称" align="center" prop="adName" />
      <el-table-column label="广告商" align="center" prop="advertiser" />
      <el-table-column label="合同开始时间" align="center" prop="startTime" />
      <el-table-column label="合同结束时间" align="center" prop="endTime" />
      <el-table-column label="状态" align="center" prop="enable">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enable === 1">启用</el-tag>
          <el-tag v-else type="warning">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['community:adposcomm:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >更换广告</el-button>
          <el-button
            v-if="scope.row.enable === 1"
            v-hasPermi="['community:adposcomm:edit']"
            size="mini"
            type="text"
            @click="disable(scope.row)"
          >禁 用</el-button>
          <el-button
            v-if="scope.row.enable === 0"
            v-hasPermi="['community:adposcomm:edit']"
            size="mini"
            type="text"
            @click="enable(scope.row)"
          >启 用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="queryParams.pageNum"-->
    <!--      :limit.sync="queryParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->

    <!-- 添加或修改广告位置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-table
        ref="adTable"
        v-loading="adLoading"
        :data="adList"
        highlight-current-row
        @select="select"
        @select-all="selectAll"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="广告名" align="center" prop="name" />
        <el-table-column label="广告商" align="center" prop="advertiser" />
        <el-table-column label="合同开始时间" align="center" prop="startTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="合同结束时间" align="center" prop="endTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="adParams.pageNum"
        :limit.sync="adParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listAdposcomm, changeAd, exportAdposcomm, updateAdposcomm } from '@/api/advert/adposcomm'
import { listResource } from '../../../api/advert/resource'

export default {
  name: 'Adposcomm',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      adLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告位置表格数据
      adposcommList: [],
      // 弹出层标题
      title: '更换广告',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      adList: [],
      params: {
        posId: null,
        originalAdId: null,
        adId: null
      },
      adParams: {
        pageNum: 1,
        pageSize: 10,
        status: '0'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询广告位置列表 */
    getList() {
      this.loading = true
      listAdposcomm().then(response => {
        this.loading = false
        this.adposcommList = response.data
        console.log(this.adposcommList)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.resetParams()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.params.posId = row.id
      this.params.originalAdId = row.resourceId
      this.open = true
      this.getAdList()
    },
    // 获取未投放的广告资源
    getAdList() {
      this.adLoading = true
      listResource(this.adParams).then(res => {
        this.adList = res.rows
        this.total = res.total
        this.adLoading = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      changeAd(this.params).then(res => {
        this.msgSuccess('修改成功')
        this.getList()
        this.open = false
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有广告位置数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportAdposcomm(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    resetParams() {
      this.params = {
        posId: null,
        originalAdId: null,
        adId: null
      }
    },
    select(selection, row) {
      if (selection.length > 1) {
        this.$refs.adTable.clearSelection()
        this.$message.info('只能选中一个，已清空上一个选中')
        this.$refs.adTable.toggleRowSelection(row, true)
      }
      this.params.adId = row.id
    },
    selectAll(selection) {
      if (selection.length > 0) {
        this.$refs.adTable.clearSelection()
        this.$message.info('只能选中一个，请点击下方选择框选择')
      }
    },
    disable(row) {
      const data = {
        id: row.id,
        enable: 0
      }
      updateAdposcomm(data).then(res => {
        this.msgSuccess('已禁用')
        this.getList()
      })
    },
    enable(row) {
      const data = {
        id: row.id,
        enable: 1
      }
      updateAdposcomm(data).then(res => {
        this.msgSuccess('已启用')
        this.getList()
      })
    }
  }
}
</script>
